﻿using QLBH.Controllers;
using QLBH.Models;
using QLBH.Utilities;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace QLBH.Modules
{
    public partial class frmAdd_Edit_Warehouse : Form
    {
        private eType eType = eType.ADD;
        private WarehouseModel model;
        public WarehouseModel Model
        {
            get { return model; }
            set { model = value; }
        }

        public frmAdd_Edit_Warehouse(eType eType)
        {
            InitializeComponent();
            this.eType = eType;
            model = new WarehouseModel();
        }
        private void frmAdd_Edit_Warehouse_Load(object sender, EventArgs e)
        {
            if (this.eType == eType.ADD)
            {
                this.Text = "Thêm Kho Hàng";
            }
            else
            {
                this.Text = "Sửa Kho Hàng";
                //load data griview to textbox
                txtName.Text = Model.Name;
                txtAddress.Text = Model.Address;
                txtComment.Text = Model.Comments;
            }
        }
        private bool CheckValidData()
        {
            try
            {
                DataInputHelper.CheckTextData(txtName.Text, "Tên Kho Hàng", 30);
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.Message, "Quản Lý Kho", MessageBoxButtons.OK, MessageBoxIcon.Error);
                txtName.SelectAll();
                txtName.Focus();
                return false;
            }
            try
            {
                DataInputHelper.CheckTextData(txtAddress.Text, "Địa Chỉ", 200);
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.Message, "Quản Lý Kho", MessageBoxButtons.OK, MessageBoxIcon.Error);
                txtAddress.SelectAll();
                txtAddress.Focus();
                return false;
            }
            try
            {
                DataInputHelper.CheckTextData(txtComment.Text, "Comment", 200);
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.Message, "Quản Lý Kho", MessageBoxButtons.OK, MessageBoxIcon.Error);
                txtComment.SelectAll();
                txtComment.Focus();
                return false;
            }
            return true;
        }
        private void buttonX1_Click(object sender, EventArgs e)
        {
            if (CheckValidData())
            {
                Model.Name = txtName.Text;
                Model.Address = txtAddress.Text;
                Model.Comments = txtComment.Text;
                //check dieu kien truoc khi chay
                if (eType == eType.ADD)
                {
                    try
                    {

                        WarehouseInsertResponse result = WSAccess.Insert<WarehouseModel, WarehouseInsertResponse>(model, MainForm.Instance.Shop_id + "/warehouse/insert"); ;
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                }
                else
                {
                    try
                    {
                        WSAccess.Update<WarehouseModel>(model, Model.Id + "/warehouse/edit");
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                }
            }
            else
            {
                this.DialogResult = DialogResult.None;
            }
        }

        private void btnHuy_Click(object sender, EventArgs e)
        {

        }

        private void labelX2_Click(object sender, EventArgs e)
        {

        }

        private void txtAddresstxtAddress_TextChanged(object sender, EventArgs e)
        {

        }
    }
    public class WarehouseInsertResponse : ResponseBase
    {
        private int warehouse_id;

        public int Warehouse_id
        {
            get { return warehouse_id; }
            set { warehouse_id = value; }
        }
    }
}
